package com.zhuss.core.auth;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.auth.AuthBo;
import com.zhuss.bean.bo.core.auth.CheckAuthBo;
import com.zhuss.bean.vo.core.auth.AuthResourceVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.util.JsonUtil;

/**
 * 权限校验 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("authCheck")
public class AuthCheck extends AbstractProcessor<Object, CheckAuthBo> implements Processor<Object, CheckAuthBo> {
	
	@Autowired
	@Qualifier("authResourceProcessor")
	private Processor<AuthResourceVo, AuthBo> authResourceProcessor;
	
	/**
     * 校验
     */
    @Override
    public void check(CheckAuthBo bo) throws OceanException {
    	
    }

    /**
     * 操作方法
     * @throws OceanException 
     */
    @Override
    public Object process(CheckAuthBo bo) throws OceanException {
    	AuthBo authBo = new AuthBo();
    	BeanUtils.copyProperties(bo, authBo);
    	AuthResourceVo vo = (AuthResourceVo) authResourceProcessor.process(authBo);
    	String url = bo.getUrl();
    	String realUrl = bo.getRealUrl();
    	
    	if (vo.checkUrl(url) || vo.checkUrl(realUrl)) {
    		return true;
    	}
        return false;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CheckAuthBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.权限校验.key());
    	record.setTargetId(param.getConcurrentUserId());
    	record.setTargetName("用户ID:" + param.getConcurrentUserId() + ", 系统:" + param.getConcurrentSystem() + ", url:" + param.getUrl());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
		return null;
	}
}
